﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Core
{
    public class SafePath
    {
        private static string CombinePath(string path1, string path2)
        {
            if(string.IsNullOrEmpty(path1))
            {
                path1 = "";
            }
            string pPath1 = path1;
            string pPath2 = path2;
            if (path1.EndsWith("/"))
            {
                pPath1 = path1.Substring(0, path1.Length - 1);
            }
            if (path2.StartsWith("/"))
            {
                pPath2 = path2.Substring(1);
            }
            return $"{pPath1}/{pPath2}";
        }

        public static string Combine(params string[] paths)
        {
            var result = paths[0];
            for (var i = 0; i < paths.Length - 1; i++)
            {
                result = CombinePath(result, paths[i + 1]);
            }
            return result;
        }

        public static bool IsAbsPath(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return false;
            }

            path = path.Replace("\\", "/");

            if(path.StartsWith("/") || path.StartsWith("."))
            {
                return false;
            }

            throw new NotImplementedException("没写完");
        }
    }

    public static class PathExtension
    {
        public static string NormalizeDirStr(this string path)
        {
           return path.Replace("\\", "/");
        }
    }
}
